import pygame
from pygame.locals import *
from pygame import *
from guiinput import guiinput
from filetype import *
#from mplayer import mplayer
from config import config
from image import image
from button import button
from label import label
from labellist import labellist
from delete import delete
from message import message
from lmenu import lmenu
from rmenu import rmenu
from status import status
from vkeyboard import vkeyboard

class init:
    def __init__(self):
        # INITIALIZE PYGAME
        pygame.init()
        # TURN MOUSE OFF
        pygame.mouse.set_visible(0)
        # GET RESOLUTION
        joyconf = config("joywm.xml")
        tmpres = joyconf.conf.get('resolution')
        mark = tmpres.find('x')
        resx = int(tmpres[0:mark])
        resy = int(tmpres[mark + 1:len(tmpres)])
        resolution = (resx, resy)
        pygame.display.set_mode(resolution)
        if resx / 4 * 3 == resy: print 'Aspect ratio: 4:3'
        if resx / 16 * 10 == resy: print 'Aspect ratio: 16:10'
        # INIT JOYSTICK(S)
        for x in range(joystick.get_count()):
            j = joystick.Joystick(x)
            print "Joystick %i: " % x + j.get_name()
            j.init()
        # FULLSCREEN?
        joyconf = config("joywm.xml")
        if joyconf.conf.get('fullscreen') == "yes":
            pygame.display.toggle_fullscreen()

class new_app(pygame.sprite.Sprite):
    # CREATES A NEW APPLICATION WITH BACKGROUND IMAGE
    def __init__(self):
        # CALL SPRITE INITIALIZER
        pygame.sprite.Sprite.__init__(self)
        # Create a list for all sprites
        self.spritelist = []
        # OPEN joywm.xml AND STORE THE STUFF
        joyconf = config("joywm.xml")
        self.keyboard = joyconf.conf.get('keyboard')
        theme = joyconf.conf.get('theme')
        themeconf = config(theme)
        resolution = joyconf.conf.get('resolution')
        mark = resolution.find('x')
        resx = int(resolution[0:mark])
        resy = int(resolution[mark + 1:len(resolution)])
        self.resolution = (resx, resy)
        self.cubex = 0
        self.cubey = 0
        self.divx = 0
        self.divy = 0
        if self.resolution[0] / 4 * 3 == self.resolution[1]:
            self.cubex = self.resolution[0] / 32
            self.cubey = self.resolution[1] / 24
            self.divx = 32
            self.divy = 24
        if self.resolution[0] / 16 * 10 == self.resolution[1]:
            self.cubex = self.resolution[0] / 40
            self.cubey = self.resolution[1] / 25
            self.divx = 40
            self.divy = 25
        # OPEN theme.xml AND STOR THE STUFF IN THIS OBJECT
        self.themename = themeconf.conf.get('name')
        self.fontcolor = (int(themeconf.conf.get('fontcolorr')), int(themeconf.conf.get('fontcolorg')), int(themeconf.conf.get('fontcolorb')))
        self.font = "themes/" + themeconf.conf.get('name') + "/" + themeconf.conf.get('font')
        self.fontbold = 0
        if themeconf.conf.get('bold') == 'True':
            self.fontbold = 1
        self.fontitalic = 0
        if themeconf.conf.get('italic') == 'True':
            self.fontitalic = 1
        self.fontsize = (self.resolution[1] / self.divy)
        tmpfs = self.fontsize # TEMP FONTSIZE FOR ADJUSTING THE CORRECT FONTSIZE
        teststring = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
        testfont = pygame.font.Font(self.font, tmpfs)
        testfont.set_bold(self.fontbold)
        testfont.set_italic(self.fontitalic)
        testrender = testfont.render(teststring, 1, self.fontcolor)
        while testrender.get_rect().height > self.fontsize:
            tmpfs -= 1
            testfont = pygame.font.Font(self.font, tmpfs)
            testfont.set_bold(self.fontbold)
            testfont.set_italic(self.fontitalic)
            testrender = testfont.render(teststring, 1, self.fontcolor)
        self.fontsize = tmpfs
        self.themepath = "themes/" + self.themename + "/"
        # Load background
        bgimage = "themes/" + self.themename + "/widgets/" + themeconf.conf.get('background')
        background = image(bgimage, 0, 0, self.divx, self.divy, self)

class update_screen:
    def __init__(self, application):
        # SOMETIMES WE NEED TO REDRAW A SURFACE, EG: WHEN THE MENU START AN APP, THE APP EXITS DUE TO USER ACTION
        # THE MENU MUST REDRAW IN ANY WAY AND THIS IS THIS CLASS FOR
        application.spritegrouprender.draw(pygame.display.get_surface())
        pygame.display.flip()
